package cn.cmcc.online.smsapi.ui.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.cmcc.online.smsapi.ui.model.SmsProgress;
import cn.cmcc.online.smsapi.ui.util.DensityUtil;

/**
 * Created by ZhangYifan on 2017/6/19.
 */

public class ProgressTC extends LinearLayout {
    private DotTextView dotTextView;
    private ProgressBarTextView progressbarTV;

    public ProgressTC(Context context) {
        super(context);

        setOrientation(VERTICAL);
        init();
    }

    public void setTitle(String title) {
        dotTextView.setTitle(title);
    }

    public void setProgress(SmsProgress smsProgress) {
        int progress = (int) (smsProgress.getLeft() / smsProgress.getTotal() * 100);
        progressbarTV.setProgress(progress);
        String leftInfo = "剩余" + smsProgress.getLeft() + smsProgress.getUnitName();
        progressbarTV.setLeftInfo(leftInfo);

    }

    private void init() {
        dotTextView = new DotTextView(getContext());
        LayoutParams titleLinearLayoutParams = new LayoutParams(-1, -2);
        titleLinearLayoutParams.setMargins(DensityUtil.dp2px(getContext(), 15), DensityUtil.dp2px(getContext(), 15), 0, 0);
        addView(dotTextView, titleLinearLayoutParams);

        progressbarTV = new ProgressBarTextView(getContext());
        LayoutParams progressLinearLayoutParams = new LayoutParams(-1, -2);
        progressLinearLayoutParams.setMargins(DensityUtil.dp2px(getContext(), 15), DensityUtil.dp2px(getContext(), 15), DensityUtil.dp2px(getContext(), 15), 0);
        addView(progressbarTV, progressLinearLayoutParams);
    }

    class ProgressBarTextView extends LinearLayout {
        private final ProgressBar progressBar;
        private final TextView tvLeftInfo;

        public ProgressBarTextView(Context context) {
            super(context);
            setOrientation(HORIZONTAL);
            setGravity(Gravity.CENTER_VERTICAL);

            progressBar = new ProgressBar(context);
            LayoutParams progressBarParams = new LayoutParams(DensityUtil.dp2px(context, 150), DensityUtil.dp2px(context, 12));
            progressBarParams.setMargins(DensityUtil.dp2px(context, 19), 0, 0, 0);
            addView(progressBar, progressBarParams);

            tvLeftInfo = new TextView(context);
            tvLeftInfo.setTextColor(Color.parseColor("#5C5C5C"));
            tvLeftInfo.setTextSize(12);
            tvLeftInfo.setGravity(Gravity.RIGHT);
            LayoutParams tvTtileParams = new LayoutParams(-1, -2);
            tvTtileParams.setMargins(DensityUtil.dp2px(context, 5), 0, 0, 0);
            addView(tvLeftInfo, tvTtileParams);
        }

        public void setProgress(int progress) {
            progressBar.setProgress(progress);
        }

        public void setLeftInfo(String leftInfo) {
            tvLeftInfo.setText(leftInfo);
        }
    }

    /**
     * 进度条
     */
    class ProgressBar extends View {
        private Context mContext;
        private int progress;
        Paint mPaint;

        public ProgressBar(Context context) {
            super(context);
            mContext = context;
            mPaint = new Paint();
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
            mPaint.setColor(Color.parseColor("#aae333"));
            mPaint.setAntiAlias(true);
        }

        //设置进度
        public void setProgress(int progress) {
            this.progress = progress;
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setColor(Color.parseColor("#eff0f5"));
            RectF r1 = new RectF(0, 0, DensityUtil.dp2px(mContext, 150), DensityUtil.dp2px(mContext, 12));
            int dpRectF = progress * 150 / 100;
            if (dpRectF > 150)
                dpRectF = 150;
            if (dpRectF < 0)
                dpRectF = 0;
            canvas.drawRoundRect(r1, DensityUtil.dp2px(mContext, 6), DensityUtil.dp2px(mContext, 6), paint);
            if (dpRectF >= 6 && dpRectF <= 12) {  //   6---12dp
                RectF oval2 = new RectF(0, 0, DensityUtil.dp2px(mContext, 12), DensityUtil.dp2px(mContext, 12));// 设置个新的长方形，扫描测量
                canvas.drawArc(oval2, 90, 180, true, mPaint);
            } else if (dpRectF <= 150 && dpRectF > 12) { //大于12dp
                RectF r2 = new RectF(0, 0, DensityUtil.dp2px(mContext, dpRectF), DensityUtil.dp2px(mContext, 12));
                canvas.drawRoundRect(r2, DensityUtil.dp2px(mContext, 6), DensityUtil.dp2px(mContext, 6), mPaint);
            }
        }
    }
}
